package com.example.service.impl;

import com.example.common.CustomException;
import com.example.dao.CategoryDao;
import com.example.dao.GoodsDao;
import com.example.entity.Category;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.example.util.Savesession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 商品服务实现类
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    
    @Resource
    private CategoryDao categoryDao;
    
    @Resource
    private Savesession savesession;

    @Override
    public PageInfo<Goods> getGoodsList(Integer pageNum, Integer pageSize, String key, String key1) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("key1", key1);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> list = goodsDao.selectAll(map);
        
        // 填充分类信息和库存信息
        for (Goods goods : list) {
            Category category = categoryDao.findById(goods.getCategoryid());
            goods.setCategory(category);
            goods.setCatename(category.getName());
            
            int kcnum = savesession.getInventory(goods.getId(), null);
            goods.setKcnum(kcnum);
        }
        
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Goods goods) {
        if (goods.getId() == null) {
            goodsDao.add(goods);
        } else {
            goodsDao.update(goods);
        }
    }

    @Override
    public void updateGoodsRecommendation(Goods goods) {
        goodsDao.update(goods);
    }

    @Override
    public void setSpecialPrice(int id, Double tjprice) {
        Goods goods = goodsDao.findById(id);
        if (goods.getPrice() < tjprice) {
            throw new CustomException("特价不能高于原价");
        }
        goods.setTjprice(tjprice);
        goodsDao.update(goods);
    }

    @Override
    public HashMap<String, Object> getGoodsDetail(int id, HttpServletRequest request) {
        HashMap<String, Object> res = new HashMap<>();
        
        // 获取商品信息
        Goods goods = goodsDao.findById(id);
        Category category = categoryDao.findById(goods.getCategoryid());
        goods.setCatename(category.getName());
        
        // 获取库存和销量信息
        int kcnum = savesession.getInventory(goods.getId(), request);
        int xlnum = savesession.getGoodsXlnum(id, request);
        goods.setXlnum(xlnum);
        goods.setKcnum(kcnum);
        
        // 随机推荐商品信息
        List<Goods> list = goodsDao.selectSj(null);
        for (Goods goods1 : list) {
            Category category1 = categoryDao.findById(goods1.getCategoryid());
            goods1.setCatename(category1.getName());
            
            int kcnum1 = savesession.getInventory(goods1.getId(), request);
            goods1.setKcnum(kcnum1);
            
            int xlnum1 = savesession.getGoodsXlnum(goods1.getId(), request);
            goods1.setXlnum(xlnum1);
        }
        
        res.put("goods", goods);
        res.put("list", list);
        
        return res;
    }

    @Override
    public void deleteGoods(int id) {
        goodsDao.delete(id);
    }

    @Override
    public void batchDeleteGoods(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            goodsDao.delete(Integer.parseInt(id));
        }
    }
}
